(function($){
	var arrayInput = [];
	$.upload = function(opciones){
		var options = $.extend({
			urlprocesa : 'null',
			repositorio : '/',
			listaArchivo : '',
			triggerUpload : ''
		},opciones);


		var n = 0,
			contador = 0,
			total_file = 0,
			divresult = document.getElementById(result_file),
			new_file = document.createElement("input");

		new_file.setAttribute("type", "file");
  		new_file.setAttribute("name", "file[]");
  		new_file.setAttribute("class", "hidden");
  		document.getElementById('frm_upload').insertBefore(new_file, divresult);

		$(new_file).click();
		$(new_file).change(function(){
			console.log('upload');
			total_file = $(':file').length;
			contador = total_file-1;

			for (var i = 0; i < 1; i++) {
				var fileSize = 0;

			    if (new_file.files[i]['size'] > 1024 * 1024) {
			       	fileSize = (Math.round(new_file.files[i]['size'] * 100 / (1024 * 1024)) / 100).toString() + 'MB';
			    } else {
			       	fileSize = (Math.round(new_file.files[i]['size'] * 100 / 1024) / 100).toString() + 'KB';
			    }

				var listafile = [
					"<div class='lista_file'>",
					"<span class='nombre_archivo'>"+new_file.files[i]['name']+"</span>",
					"<span class='tamano_archivo'>"+fileSize+"</span>",
					"<span class='estado_archivo'><div class='progressbar progressbar-blue'>",
        			"<div class='progressbar-inner-"+contador+"'></div>",
  					"</div></span>",
					"</div>"
				].join('');

				$('#'+opciones.listaArchivo).append(listafile);
			}
		});

		$('#'+opciones.triggerUpload).click(function(){
			if (n < 1) {
				data = new FormData();

				for (var i = 0; i < total_file; i++) {
					data.append('archivo[]',new_file.files[i]);
					data.append('path',opciones.repositorio);

					var xhr = new XMLHttpRequest();
						percentComplete = 0,
						clase = '.progressbar-inner-'+i;

					xhr.upload.addEventListener("progress", function(e){
						if (e.lengthComputable) {
							var percentComplete = Math.ceil((e.loaded/e.total) * 100);
							$(clase).css({'width':percentComplete-4+'%', 'padding-right':'5%'});
							$(clase).text(percentComplete+'%');
						} else {
						   	document.getElementById('progressNumber').innerHTML = 'unable to compute';
						};
					});

					xhr.upload.addEventListener("load", function(e){
						$(clase).css({'width':'96%', 'padding-right':'5%'});
						$(clase).text('100%');
					})

					xhr.upload.addEventListener("error", function(e){
						alert('Error en upload');
					})
				}
				xhr.open('POST', opciones.urlprocesa);
				xhr.setRequestHeader('Cache-Control','no-cache');
				xhr.onreadystatechange=function() {
					if (xhr.readyState==4) {
						var result = document.createElement("div");
						result.setAttribute("class", "hidden");
						result.innerHTML = xhr.responseText;
						document.getElementById('frm_upload').insertBefore(result, divresult);
						console.log(xhr.responseText);
					}
				}
				xhr.send(data);
			};
			n++;
		});
	}
})(jQuery);